﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dbComponentGenerator;
using Shohdi.DataHandler;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.IO;

namespace AllSqlServerDBComponentGenerator
{
    public class GenerateTables
    {
        /// <summary>
        /// this function will get all tables and views from database
        /// </summary>
        /// <param name="_Connection">the connection to database</param>
        /// <param name="_Provider">the provider of the connection</param>
        /// <returns>the tables and views in a list</returns>
        public static List<string> getTables(string _Connection, string _Provider)
        {
            ClassDataHandler con = new ClassDataHandler(_Connection, _Provider);

            List<string> lstRet = new List<string>();

            con.OpenConnection();


            string strQuery = " select * from dbo.[Sysobjects] where [type] = 'U' or [type] = 'V' ";


            
            DbDataReader dr =  con.SelectDataReader(strQuery, CommandType.Text);


            if (dr != null)
            {
                if (dr.HasRows)
                {

                    while (dr.Read())
                    {
                        lstRet.Add(dr["name"].ToString());
                    }
                }
            }



            con.CloseConnection();
            return lstRet;
        }

        /// <summary>
        /// this function will generate files from a table list
        /// </summary>
        /// <param name="strPath">the path to save files to</param>
        /// <param name="lstTables">the tables list</param>
        /// <param name="_Connection">the connection to database</param>
        /// <param name="_Provider">the provider of connection</param>
        public static void generateFiles(string strPath, List<string> lstTables, string _Connection, string _Provider)
        {

            string generatedPath = strPath + "Generated\\";
            if (!Directory.Exists(generatedPath))
                Directory.CreateDirectory(generatedPath);


            for (int i = 0; i < lstTables.Count; i++)
            {
                try
                {

                    string strRet = dbComponentGenerater.GenerateTableClass(lstTables[i], _Connection, _Provider);
                    File.WriteAllText(generatedPath + lstTables[i].Replace(" ", "_") + ".generated.cs", strRet);

                    strRet = dbComponentGenerater.GenerateTableAdapter(lstTables[i], _Connection, _Provider);
                    File.WriteAllText(generatedPath + lstTables[i].Replace(" ", "_") + "DB.generated.cs", strRet);


                    strRet = dbComponentGenerater.GenerateTableClassEmpty(lstTables[i], _Connection, _Provider);
                    File.WriteAllText(strPath + lstTables[i].Replace(" ", "_") + ".cs", strRet);

                    strRet = dbComponentGenerater.GenerateTableAdapterEmpty(lstTables[i], _Connection, _Provider);
                    File.WriteAllText(strPath + lstTables[i].Replace(" ", "_") + "DB.cs", strRet);



                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }


    }
}
